<?php

include_once ('class.fileLog.php');
include_once ('configure.php');

class Db
{
  
  private $link;
  private $str_err;
  
  /** Costrutture della classe
  * @param void.
  */
  function Db()
  {
    $str_err="";
    $link=null;
  }
  
  /** Connessione DB nome Hostname, user, password
  * sono configurate nel file configure.php
  * @param void.
  */
  function connect() 
  {
      $connect      = mysql_connect(MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
      $select_db    = mysql_select_db (MYSQL_DATABASE, $connect); 
      if (!$connect) {
	 $errno  = mysql_errno();
	 switch($errno) {
		case 1045 : { $this->error(__FILE__.":".__LINE__." "); break; }
	      }
      }
      $this->link=$connect;
  }
  
  /** Chiusara connessione DB
  * @param void.
  */
  function disconnettiDB()
  {
      mysql_close($this->link);
  }
  
  /** Gestione error salvati nel log_file 
  * classe class.fileLog.php
  * @param error_text
  */
  function error($text) {
     $log=new FileLog();
     $log->writeLog("class.db.php function connect(): MYSQL SERVER ERROR :".$text);
     $log->close();
  }
  
  /** funzione execute().\n
  * Viene usata per INSERT, UPDATE, DELETE...
  * @param $query.
  * @param ritorna resource 
  */
  function execute($query,$log = true)
  {
      if($log)
        $result=mysql_query($query,$this->link) or $this->error(__FILE__.":".__LINE__." ".$query);
      else
      {
        @mysql_query($query,$this->link);
      }
      return $result;
  }
  
  /** funzione execute_select().\n
  * Viene usata per SELECT
  * @param $query.
  * @param ritorna matrice[righe][colonne]
  */
  function execute_select($query)
  {
      $point=$this->execute($query);
      while($tmp = mysql_fetch_row($point))
      {
          $result[]=$tmp;
      } 
      return $result;
  }
 }  
?>  